package com.ruoyi.binance.task;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.binance.api.KlinesApi;
import com.ruoyi.binance.domain.*;
import com.ruoyi.binance.mapper.*;
import com.ruoyi.binance.service.IBinanceMinKConfService;
import com.ruoyi.binance.service.IBinanceMinKService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Component
public class MinKTask {

    private static final Logger log = LoggerFactory.getLogger(MinKTask.class);
    @Autowired
    private IBinanceMinKService binanceMinKService;
    @Autowired
    private IBinanceMinKConfService binanceMinKConfService;
    @Autowired
    private Binance15minAverageMapper binance15minAverageMapper;
    @Autowired
    private Binance30minAverageMapper binance30minAverageMapper;
    @Autowired
    private BinanceHourAverageMapper binanceHourAverageMapper;
    @Autowired
    private BinanceMinKMapper stockMinKMapper;
    @Autowired
    private BinanceMinAverageMapper stockMinAverageMapper;
    @Autowired
    private Binance15minDayAveMapper binance15minDayAveMapper;
    @Autowired
    private MonitoringTask monitoringTask;

    @Scheduled(cron = "0/15 * * * * ?")
    public void minK() {
        try {
            log.info("分时K线定时任务----------------------开始");
            List<BinanceMinKConf> priceMonitorings = binanceMinKConfService.selectBinanceMinKConfList(new BinanceMinKConf());
            for (BinanceMinKConf bean : priceMonitorings) {
                JSONArray jsonArray = KlinesApi.minK(bean.getCode(), null, null);
                if (jsonArray.isEmpty()) {
                    continue;
                }
                binanceMinKService.saveOrUpdateMinK(bean.getCode(), jsonArray);
                Date time = jsonArray.getJSONArray(jsonArray.size() - 1).getDate(0);
                BigDecimal close = jsonArray.getJSONArray(jsonArray.size() - 1).getBigDecimal(4);
                BigDecimal open = jsonArray.getJSONArray(jsonArray.size() - 1).getBigDecimal(1);
                String code = bean.getCode();
                //计算日均价
                BigDecimal bigDecimal = stockMinKMapper.selectMinAveragePrice(code);
                BigDecimal averagePriceDay = bigDecimal == null ? close : bigDecimal.setScale(8, RoundingMode.HALF_UP);
                BinanceMinAverage stockMinAverage = new BinanceMinAverage();
                stockMinAverage.setCode(code);
                stockMinAverage.setTime(time);
                stockMinAverage.setPrice(averagePriceDay);
                int i = stockMinAverageMapper.updateBinanceMinAverage(stockMinAverage);
                if (i == 0) {
                    stockMinAverageMapper.insertBinanceMinAverage(stockMinAverage);
                }
                //计算15分钟均价
                BigDecimal bigDecimal15 = stockMinKMapper.select15MinAveragePrice(code);
                BigDecimal averagePrice15 = bigDecimal15 == null ? close : bigDecimal15.setScale(8, RoundingMode.HALF_UP);
                Binance15minAverage stock15minAverage = new Binance15minAverage();
                stock15minAverage.setCode(code);
                stock15minAverage.setTime(time);
                stock15minAverage.setPrice(averagePrice15);
                int i15 = binance15minAverageMapper.updateBinance15minAverage(stock15minAverage);
                if (i15 == 0) {
                    binance15minAverageMapper.insertBinance15minAverage(stock15minAverage);
                }
                //计算30分钟均价
                BigDecimal bigDecimal30 = stockMinKMapper.select30MinAveragePrice(code);
                BigDecimal averagePrice30 = bigDecimal30 == null ? close : bigDecimal30.setScale(8, RoundingMode.HALF_UP);
                Binance30minAverage stock30minAverage = new Binance30minAverage();
                stock30minAverage.setCode(code);
                stock30minAverage.setTime(time);
                stock30minAverage.setPrice(averagePrice30);
                int i30 = binance30minAverageMapper.updateBinance30minAverage(stock30minAverage);
                if (i30 == 0) {
                    binance30minAverageMapper.insertBinance30minAverage(stock30minAverage);
                }
                //计算小时均价
                BigDecimal bigDecimalHour = stockMinKMapper.selectHourAveragePrice(code);
                BigDecimal averagePriceHour = bigDecimalHour == null ? close : bigDecimalHour.setScale(8, RoundingMode.HALF_UP);
                BinanceHourAverage stockHourAverage = new BinanceHourAverage();
                stockHourAverage.setCode(code);
                stockHourAverage.setTime(time);
                stockHourAverage.setPrice(averagePriceHour);
                int iHour = binanceHourAverageMapper.updateBinanceHourAverage(stockHourAverage);
                if (iHour == 0) {
                    binanceHourAverageMapper.insertBinanceHourAverage(stockHourAverage);
                }
                //计算15分-24小时算术平均
                BigDecimal bigDecimal15_day = binance15minAverageMapper.selectBinance15minDayAve(code);
                BigDecimal averagePrice15_day = bigDecimal15_day == null ? close : bigDecimal15_day.setScale(8, RoundingMode.HALF_UP);
                Binance15minDayAve binance15minDayAve = new Binance15minDayAve();
                binance15minDayAve.setCode(code);
                binance15minDayAve.setTime(time);
                binance15minDayAve.setPrice(averagePrice15_day);
                int i1 = binance15minDayAveMapper.updateBinance15minDayAve(binance15minDayAve);
                if (i1 == 0) {
                    binance15minDayAveMapper.insertBinance15minDayAve(binance15minDayAve);
                }
                //执行监控
                monitoringTask.monitoring(code, time, close, open);
            }
            log.info("分时K线定时任务----------------------结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
